<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="modal fade in" id="selAgentModal" tabindex="-1" aria-hidden="false" style="display: none;">
    <div class="modal-dialog modal-lg" style="width: 850px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">选择代录入人</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="page-search">
                            <input type="hidden" id="selLoanIds">
                            <form id="selAgentForm" class="form-inline">
                                <div class="form-group">
                                    <label>工号：</label>
                                    <input name="jobNum" value="" class="input-sm  form-control" type="text">
                                </div>

                                <div class="space-2"></div>
                                <div class="row center">
                                    <button type="button" id="queryUserBtn" class="btn btn-purple btn-sm no-border">
                                        <i class="ace-icon fa fa-search fa-on-right"></i>查询
                                    </button>
                                    <button type="reset" class="btn btn-default btn-sm no-border">
                                        <i class="ace-icon fa fa fa-lightbulb-o"></i>清空
                                    </button>
                                </div>
                            </form>
                        </div>

                        <div class="space-4"></div>
                        <div class="col-xs-12">
                            <div id="jqxgridUser"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <%--<button type="button" id="queryUserBtn" class="btn btn-primary">确定</button>--%>
                <button type="button" data-dismiss="modal" class="btn btn-default">
                    <i class="ace-icon fa fa-times bigger-110"></i> 关闭
                </button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    function showselAgentModal(id) {
        $("#selAgentModal").modal("show");
    }

    $(document).ready(function () {
        var source = {
            datatype: 'json',
            datafields: [
                {name: 'id', type: 'string'},
                {name: 'username', type: 'string'},
                {name: 'realName', type: 'string'},
                {name: 'phone', type: 'string'},
                {
                    name: '操作',
                    type: 'string'
                }
            ],
            id: 'id',
            url: '${contextPath}/sys/user/listBusiHandler',
            type: 'POST',
            async: true,
            filter: function () {
                $("#jqxgridUser").jqxGrid('updatebounddata', 'filter');
            },
            beforeprocessing: function (data) {
                if (data != null) {
                    source.totalrecords = data.totalElements;
                }
            }
        };

        var jqxgridUser = $('#jqxgridUser');
        var dataAdapter = new $.jqx.dataAdapter(source, {
            formatData: function (data) {
                $.extend(data, {
                    params: JSON.stringify($("#selAgentForm").serializeObject())
                });
                return data;
            }
        });
        jqxgridUser.jqxGrid({
            theme: 'bootstrap',
            width: '100%',
            autoheight: true,
            source: dataAdapter,
            pageable: true,
            filterable: false,
            enablebrowserselection: true,
            columnsresize: true,
            pagesize: 10,
            pagesizeoptions: ['10', '15', '20'],
            virtualmode: true,
            rendergridrows: function (obj) {
                return obj.data;
            },
            columns: [
                {text: '工号', datafield: 'username'},
                {text: '姓名', datafield: 'realName'},
//                {text: '手机号', datafield: 'phone'},
                {text: '操作', datafield: '操作', columntype: 'string', cellsrenderer: selUser}
            ]
        });

        jqxgridUser.on('bindingcomplete', function (event) {
            $("#jqxgridUser").jqxGrid('localizestrings', localizationobj);
        });

        $("#queryUserBtn").click(function () {
            $('#jqxgridUser').jqxGrid('updatebounddata');
        });
    });

    var selUser = function (row, column, value) {
        var data = $("#jqxgridUser").jqxGrid('getrowdata', row);
        if (data == undefined) {
            return "";
        }
        var btn = "&nbsp";
        btn += "<button class='btn btn-xs btn-success' onclick='selectedUser(\"" + data.username + "\")'>选中</a> ";
        return btn;
    };

    function selectedUser(username) {
        $.ajax({
            type: "POST",
            url: "${contextPath}/sys/user/pointAgent",
            data: {
                loanIds: $("#selLoanIds").val(),
                agent: username
            },
            async: false,
            success: function (data) {
                $("#selAgentModal").modal("hide");
                if (data.success) {
                    window.location.reload();
                }
                else {
                    layer.msg(data.msg);
                }
            }
        })
    }

    function cancelDispatch() {
        layer.confirm('确定取消？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                type: "POST",
                url: "${contextPath}/sys/user/cancelAgent",
                data: {
                    loanIds: $("#selLoanIds").val()
                },
                async: false,
                success: function (data) {
                    $("#selAgentModal").modal("hide");
                    if (data.success) {
                        window.location.reload();
                    }
                    else {
                        layer.msg(data.msg);
                    }
                }
            })
        }, function () {

        });
    }
</script>

